from PIL import Image
img = Image.open(r'N:\bbbbbbbbbbbbcccccccccccccccccc\Data\pythonProject\毕业设计数据集\test_denoise_gaussian41.png')
print(img.mode)
import numpy as np
import cv2
def add_gaussian_noise(image, mean=2, std_dev=10):
    noise_image = np.random.normal(mean, std_dev, image.shape)
    noisy_image = image + noise_image
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image

def add_salt_and_pepper_noise(image, density=0.05):
    noise_image = np.copy(image)
    h, w, c = image.shape
    num_salt = int(density * h * w)
    coords = [np.random.randint(0, i - 1, num_salt) for i in image.shape]
    noise_image[tuple(coords[:-1]) + (0,)] = 255
    noise_image[tuple(coords[:-1]) + (1,)] = 255
    noise_image[tuple(coords[:-1]) + (2,)] = 255
    num_pepper = int(density * h * w)
    coords = [np.random.randint(0, i - 1, num_pepper) for i in image.shape]
    noise_image[tuple(coords[:-1]) + (0,)] = 0
    noise_image[tuple(coords[:-1]) + (1,)] = 0
    noise_image[tuple(coords[:-1]) + (2,)] = 0
    return noise_image


def add_uniform_noise(image, low=0, high=255):  #均匀分布噪声
    noise_image = np.random.uniform(low, high, image.shape)
    noisy_image = image + noise_image
    noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)
    return noisy_image


# 加载原始图片
# image = np.array(img)
# # 检查图片是否成功加载
# if image is None:
#     print("Failed to load image file")
# else:
#     # 显示图片
#     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#     cv2.imshow("Original Image", image)
#     cv2.waitKey(0)
#     cv2.destroyAllWindows()
# # 添加高斯噪声并保存
# noisy_image_gaussian = add_gaussian_noise(image, std_dev=30)
# cv2.imwrite(r"N:\bbbbbbbbbbbbcccccccccccccccccc\Data\pythonProject\test_denoise_gaussian.png", noisy_image_gaussian)
#
# noisy_image_salt_and_pepper= add_salt_and_pepper_noise(image)
# cv2.imwrite(r"N:\bbbbbbbbbbbbcccccccccccccccccc\Data\pythonProject\test_denoise_salt_and_pepper.png", noisy_image_salt_and_pepper)

# noisy_image_uniform = add_uniform_noise(image, low=20, high=230)
# cv2.imwrite(r"N:\Program\Data\pythonProject\test_denoise_uniform.png", noisy_image_uniform)
#其中，mean表示高斯噪声的均值，std_dev表示标准差，可以根据实际情况进行调整。在上述示例中，
# 使用了NumPy库的np.random.normal函数生成高斯噪声，并使用OpenCV库的cv2.imread和cv2.imwrite函数读取原始图片和保存添加了噪声的图片。

from PIL import Image

# # 打开原始图片
# img = Image.open(r"N:\Program\Data\pythonProject\毕业设计数据集\test.jpg")
# # 获取原图宽高
# width, height = img.size
# # 设置降低后的宽高
# new_width, new_height = width / 2, height / 2   # 示例中将宽高都降低一半，可以根据实际需求调整数值
# # 降低图片分辨率
# new_img = img.resize((int(new_width), int(new_height)), Image.LANCZOS)  # 使用Lanczos算法resize图片，并转换为整数
# # 保存处理后的图片
# new_img.save("new_img.jpg")
# #以上代码中，首先使用Image.open()方法打开原始图片，然后使用size属性获取原图的宽高。接着设置降低后的宽高，这里将原图宽高都降低一半。最后使用resize()方法指定新的宽高，
# # 并采用抗锯齿算法进行图片压缩处理，得到处理后的新图片，并使用save()方法保存处理后的图片。


# rgba->rgb
# from PIL import Image
#
# rgba_image = Image.open(r'N:\bbbbbbbbbbbbcccccccccccccccccc\Data\pythonProject\毕业设计数据集\F16_GT4.png').convert('RGBA')
# rgb_image = Image.new("RGB", rgba_image.size, (255, 255, 255))
# rgb_image.paste(rgba_image, mask=rgba_image.split()[3])
# rgb_image.save("output_image.jpg", "JPEG")
# print(rgb_image.mode)